<?php


namespace App\frontend\services\coupon;


use App\frontend\models\AreaModel;
use App\frontend\models\CouponModel;
use App\frontend\models\CouponUseLogModel;
use function PHPUnit\Framework\exactly;

class CouponServices
{

    /**
     * 获取 - 卡券列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getCouponList($data)
    {

        $page = !isset($data['page']) ? 1 : $data['page'];
        unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];
        unset($data['limit']);

        $where_data = [];
        $where_data[] = ['is_deleted', '=', 0];

        $list = CouponModel::listed($page, $limit, $where_data);
        foreach ($list['data'] as $v) {
            if ($v->type == 1) {
                //通用券显示行业
                $v->shop_name = ['某某圈子/分类/行业'];
                $v->goods_name = ['全场通用'];
            } else {
                //专用券显示门店
                $v->shop_id;
                $shopIds = explode(',', $v->shop_id);
                $shopName = CouponUseLogModel::getShopName($shopIds);
                $v->shop_name = array_column($shopName, 'name');

                $confineIds = explode(',', $v->confine_id);
                switch ($v->confine_type) {
                    case 1:
                        $v->goods_name = ['全场通用'];
                        break;
                    case 2: //商品
                        $goodsName = CouponUseLogModel::getGoodsName($confineIds);
                        $v->goods_name = array_column($goodsName, 'name');
                        break;
                    case 3: //分类
                        $categoryName = CouponUseLogModel::getCategoryName($confineIds);
                        $v->goods_name = array_column($categoryName, 'name');
                        break;
                    case 4: //分组
                        $groupName = CouponUseLogModel::getGroupName($confineIds);
                        $v->goods_name = array_column($groupName, 'name');
                        break;
                    default:
                        $v->goods_name = [];
                        break;
                }

                $exceptIds = explode(',', $v->except_id);
                switch ($v->except_type) {
                    case 1:
                        $v->except_goods_name = ['全场通用'];
                        break;
                    case 2: //商品
                        $goodsName = CouponUseLogModel::getGoodsName($exceptIds);
                        $v->except_goods_name = array_column($goodsName, 'name');
                        break;
                    case 3: //分类
                        $categoryName = CouponUseLogModel::getCategoryName($exceptIds);
                        $v->except_goods_name = array_column($categoryName, 'name');
                        break;
                    case 4: //分组
                        $groupName = CouponUseLogModel::getGroupName($exceptIds);
                        $v->except_goods_name = array_column($groupName, 'name');
                        break;
                    default:
                        $v->except_goods_name = [];
                        break;
                }
            }

        }

        return $list;
    }

    /**
     * 获取 - 卡券详情
     *
     * @param $data
     * @return array
     */
    public static function getCouponDetail($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['id']) ? $data['id'] : 0;
        $data['is_deleted'] = 0;

        $list = CouponModel::detail($data);
        if (empty($list) && $list == false) {
            return [];
        }
        //处理数据
        $returnData['id'] = $list->id;
        $returnData['name'] = $list->name;
        $returnData['type'] = $list->type;
        $returnData['category'] = $list->category;
        $returnData['shop_id'] = $list->shop_id;
        $returnData['satisfy_money'] = $list->satisfy_money;
        $returnData['remitted_money'] = $list->remitted_money;
        $returnData['price'] = $list->price;

        $returnData['use_time_start'] = empty($list->use_time_start) ? '' : date('Y-m-d H:i', $list->use_time_start);
        $returnData['use_time_end'] = empty($list->use_time_end) ? '' : date('Y-m-d H:i', $list->use_time_end);
        //已用数量
        $useNumber = CouponUseLogModel::getUseNumber(['coupon_id' => $list->id]);
        //剩余数量
        $returnData['residue'] = empty($list->stock) ? 0 : ($list->stock - $useNumber);
        $returnData['content'] = $list->content;

        return $returnData;
    }

    /**
     * 购买卡券
     *
     * @param $data
     */
    public static function sendCoupon($data)
    {
        $member_id = explode(',', $data['member_id']);
        $number = $data['number'];
        if (empty($member_id) || $number <= 0) {
            return [];
        }
        $coupon_id = $data['coupon_id'];

        //获取卡券信息
        $couponDetail = CouponModel::detail(['id' => $coupon_id]);

        //判断卡券发放数量
        if ($couponDetail->stock > 0) {
            $useNumber = CouponUseLogModel::getUseNumber(['coupon_id' => $coupon_id]);
            if (($useNumber + ($number * count($member_id))) > $couponDetail->stock) {
                return ['msg' => '发券数量超过库存'];
            }
        }

        foreach ($member_id as $v) {
            $addData = [];
            if ($couponDetail->get_limit_max > 0) {
                $memberUseNumber = CouponUseLogModel::getUseNumber(['coupon_id' => $coupon_id, 'member_id' => $v]);
                if (($memberUseNumber + $number) > $couponDetail->get_limit_max) {
                    return ['msg' => '发券数量超过单人限领数量'];
                }
            }
            for ($i = 0; $i < $number; $i++) {
                $addData[] = [
                    'coupon_id' => $coupon_id,
                    'type' => $couponDetail->type,
                    'get_type' => 1,
                    'use_time_end' => $couponDetail->use_time_end,
                    'member_id' => $v,
                    'created_at' => time()
                ];
            }
            $list = CouponUseLogModel::insertData($addData);
        }
        return $list;
    }


    public static function getUserCouponList($data)
    {

        $page = !isset($data['page']) ? 1 : $data['page'];
        unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];
        unset($data['limit']);

        $where_data = [];
        $where_data[] = ['member_id', '=', $data['member_id']];

        switch ($data['tag']) {
            case 1: //全部

                break;
            case 2: //待使用
                $where_data[] = ['is_use', '=', 0];
                $where_data[] = ['coupon.use_time_end', '>', time()];
                break;
            case 3: //已使用
                $where_data[] = ['is_use', '=', 1];
                break;
            case 4: //已过期
                $where_data[] = ['is_use', '=', 0];
                $where_data[] = ['coupon.use_time_end', '<', time()];
                break;
        }

        $list = CouponUseLogModel::listed($page, $limit, $where_data);

        foreach ($list['data'] as $v) {
            $v->use_time_start = empty($v->use_time_start) ? '' : date('Y-m-d', $v->use_time_start);
            $v->use_time_end = empty($v->use_time_end) ? '' : date('Y-m-d', $v->use_time_end);
        }

        return $list;
    }
}
